#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import os
import sys
import json

try:
    reload(sys)
    sys.setdefaultencoding("utf8")
except:
    pass

from fabric.api import *

host1 = "root@222.180.198.30:3000"

env.passwords = {
    host1: "pd12345678"
}

GITLAB_ROOT = "/home/gitlab-codes/gitlab"


@hosts([host1])
def update_code(git_address=""):
    addresses = git_address.split("/")
    git_name = None
    if addresses:
        if ".git" in addresses[-1]:
            git_name = addresses[-1].replace(".git", "")
        else:
            print("ERROR:上传地址格式有误")
            return
    else:
        print("ERROR:上传地址格式有误")
        return

    with cd(GITLAB_ROOT):
        result = run("ls")
        result = deal_ls(result)
        if git_name in result:
            with cd(GITLAB_ROOT + "/%s" % git_name):
                result = run("git pull")
                print("GIT INFO: %s" % result)
        else:
            result = run("git clone %s" % git_address)
            print("GIT INFO: %s" % result)
    with cd(GITLAB_ROOT):
        result = run("rm -rf ./%s_physic.zip %s_aws.zip" % (git_name,git_name))
        result = run("zip -r ./%s_physic.zip %s" % (git_name, git_name))

    with cd("/home/gitlab-codes/gitlab/%s" % git_name):
        result = run("zip -r ../%s_aws.zip *" % (git_name))


@hosts([host1])
def aws_deploy(git_name=""):
    with cd(GITLAB_ROOT):
        result = run("ls %s_aws.zip" % git_name)
        result = deal_ls(result)
        if result:
            result = run("aws lambda update-function-code --function-name %s --zip-file fileb://%s_aws.zip" % (
                git_name, git_name))
        else:
            print("无项目部署代码")


@hosts([host1])
def physic_deploy(git_name=""):
    try:
        os.mkdir("../code_deploy")
    except:
        pass
    with cd(GITLAB_ROOT):
        result = run("ls %s_physic.zip" % git_name)
        result = deal_ls(result)
        if result:
            get("%s_physic.zip" % git_name, "../code_deploy")

    result = os.listdir("../code_deploy")
    if git_name in result:
        os.system("cd ../code_deploy/%s && sh bin/stop.sh")
        os.system("cd ../code_deploy && rm -rf %s" % git_name)

    os.system("cd ../code_deploy && unzip %s_physic.zip" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix *" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix */*" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix */*/*" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix */*/*/*" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix */*/*/*/*" % git_name)
    os.system("cd ../code_deploy/%s && dos2unix */*/*/*/*/*" % git_name)


@hosts([host1])
def restart_physic_env(git_name=""):
    os.system("cd ../code_deploy/%s && sh bin/stop.sh" % git_name)
    os.system("cd ../code_deploy/%s && sh bin/start.sh" % git_name)


@hosts([host1])
def stop_physic_env(git_name=""):
    os.system("cd ../code_deploy/%s && sh bin/stop.sh" % git_name)


def deal_ls(ls_result):
    result = ls_result.replace("\t", " ").replace("\n", " ").replace("\r", " ").split(" ")
    out = []
    for i in result:
        out.append(i.strip())
    return out
